<?php
/**
 * @link http://www.anabas.tk/awc/wiki
 *
 * @package Aba Sulution
 * @subpackage Aba_Core
 * @since Aba Core 1.0.1 beta
 */

defined( 'ABSPATH' ) or die(/*Nothing...*/);

abstract class AWC_Dashboad extends AWC_Settings_API implements IAWC_API{
    public $id              = '';
    
    public $id_base       = 'awc_dashboard_';
    
    public $method_title    = '';
    
    public $add_only        = false;
    
    public $active          = true;
    
    public $args            = array(
        'bcontainer'    => '',
        'econtainer'    => '',
        'bfield'        => '<p>',
        'efield'        => '</p>',
        'blabel'        => '',
        'elabel'        => '',
        'bop'           => '',
        'eop'           => '',
        'hide_title'    => true,
    );
    
    function __construct( $title ){
        $this->method_title = $title;
    }
    
    function is_active(){
        return $this->active;
    }
    
    function trigger(){
        if ( !$this->is_active() )
            return false;
            
        add_action('wp_dashboard_setup', array( $this, 'init' ) );
        return true;
    }
    
    function init(){
        
        
        
        if ( empty( $this->method_title ) )
            $this->method_title =   __( 'AWC Dashboard', AWC_LANG );
        if ( empty( $this->id ) ){
            //$this->id           =  'awc_' . substr( md5( uniqid() ), 0 ,6 );
            //throw new Exception( 'Dashboard widget ID is empty.' );
            $this->id = strtolower( get_class( $this ) );
        }
        $this->init_form();
        
        $this->init_settings();
        
        if ( count( $this->form_fields ) > 0 )
            $config_callback = array( $this, 'config' );
        else
            $config_callback = null;

        //Register the widget...
        wp_add_dashboard_widget(
            $this->id_base . $this->id,
            $this->method_title,
            array( $this, 'widget' ),
            $config_callback,
            array( $this->settings )
        );
    }
    
    function output( $configs ){
        die( "@override function widget()" );
    }
    
    function widget( $post, $intance ){
        $args = $intance['args'];
        call_user_func_array( array($this, 'output'), $args );
    }
    
    function config(){
        $this->process_admin_options();
        $this->admin_options();
    }
    
    function init_settings(){
        if ( !$this->settings )
            $this->settings = $this->get_dashboard_widget_options( $this->id_base . $this->id );
            
        if ( !$this->settings || !is_array( $this->settings ) ){
            $this->settings = array();
            
            if ( $this->form_fields ){
                foreach( $this->form_fields as $k => $v ){
                    $this->settings[ $k ] = isset( $v ['default'] )? $v ['default'] : '';
                }
            }
        }
        
        if ( $this->settings && is_array( $this->settings ) ) {
           $this->settings = array_map( array( $this, 'format_settings' ), $this->settings );
           $this->enabled  = isset( $this->settings['enabled'] ) && $this->settings['enabled'] == 'yes' ? 'yes' : 'no';
        }
    }
    
    protected function get_dashboard_widget_options( $widget_id='' ){
        $opts = get_option( 'dashboard_widget_options' );
        
        if ( empty( $widget_id ) )
            return $opts;
            
        if ( isset( $opts[$widget_id] ) )
            return $opts[$widget_id];

        return false;
    }
    
    function process_admin_options(){
        $this->validate_settings_fields();
        
        if ( count( $this->errors ) > 0 ){
            return false;
        }
        else{
            $this->update_dashboard_widget_options( $this->id_base . $this->id, $this->sanitized_fields, $this->add_only );
        }
    }
    
    protected function update_dashboard_widget_options( $widget_id , $args=array(), $add_only=false ){
        $opts = get_option( 'dashboard_widget_options' );
        
        $w_opts = ( isset( $opts[$widget_id] ) ) ? $opts[$widget_id] : array();

        if ( $add_only ) {
            $opts[$widget_id] = array_merge($args,$w_opts);
        }
        else {
            $opts[$widget_id] = array_merge($w_opts,$args);
        }
        
        return update_option('dashboard_widget_options', $opts);
    }
}